from mysql import MySql,config
from utils.commonUtil import get_bs4_obj
import re
import math

'''
conn = pymysql.connect(**config)
cur = conn.cursor()
#cur.execute("USE scraping")
cur.execute("SELECT * FROM pages WHERE id=2")

print(cur.fetchone())
cur.close()
conn.close()
'''

mysql = MySql(**config)

#print(mysql.selectOne('pages',"*","1"))

#mysql.insert("pages",title="python insert222",content="python data content333")
#mysql.insertMultiple("pages",['title',"content"],['明月几时有','把酒问青天喂 哈哈'])
'''
mysql.update("pages",{"title":"修改的明月",'content':'修改的内容'},{"id":22})
result = mysql.selectAll('pages',['id','title','content'],"1")
mysql.delete("pages",{"id":21})
print(type(result))

with open("mycontent.txt",'a') as f:
    f.write("我写入的内容")

'''

def analyseLinkTag(linkTag):
    url = '--无url--'
    name = "--无名称--"
    img_url = '--无图片--'
    if linkTag: 
        
        url = linkTag.attrs['href'] if linkTag.attrs['href'] else url
        name = linkTag.attrs['title'] if linkTag.attrs['title'] else name
        imgTag = linkTag.find("img")
        if imgTag:
            img_url = imgTag.attrs['data-src'] if imgTag.attrs['data-src'] else img_url

    return url,name,img_url

def get_37games(url):
    '''
        获取37首页的游戏信息保存到数据库
    '''
    bs4 = get_bs4_obj(url)
    divs = bs4.findAll("div",{"class":re.compile("all-game-img")})
    #divs = list(divs)
    gameLen = len(divs)
    gameLen = min(2,gameLen)
    insertData = []
    for i in range(0,gameLen):        
        divTag = divs[i]
        url,name,img_url = analyseLinkTag(divTag.find('a'))
        descDiv = divTag.find("div",{"class":re.compile("all-game-txt")})
        desc = ""
        if descDiv:
            desc = descDiv.get_text()    
        data = mysql.selectOne("game_info",'id',"name='"+name+"'")    
        if not data:
            rows = [name,url,img_url,desc]
            insertData.append(rows)            
        
    mysql.isLog = True
    fields = ['name','url','img_url','game_desc']
    effectNum = 0
    print('insertData len',len(insertData))
    effectNum = mysql.insertMultiple("game_info",fields,insertData)
    print("effectNum",effectNum)

url = "http://www.37.com"

get_37games(url)